package com.cjy.service.Impl;

import com.cjy.dao.CommentRepository;
import com.cjy.pojo.Comment;
import com.cjy.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Value("${fastdfs_domaint}")
    private String fastdfsDomaint;

    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        Sort createTime = new Sort(Sort.Direction.DESC, "createTime");
        // 根据博客id查到的所有该博客评论
        List<Comment> comments = commentRepository.findByBlogIdAndParentCommentNull(blogId, createTime);
        return eachComment(comments);
    }

    // 树结构这里用的两层
    private List<Comment> eachComment(List<Comment> comments) {
        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment:comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment,c);
            c.setRealAvatar(fastdfsDomaint + c.getAvatar());
            commentsView.add(c);
        }
        combineChildren(commentsView);
        return commentsView;
    }

    private void combineChildren(List<Comment> commentsView) {
        for (Comment comment:commentsView) {
            // 父节点下的回复数据集合
            List<Comment> replys1 = comment.getReplyComments();
            for (Comment reply1:replys1) {
                reply1.setRealAvatar(fastdfsDomaint + reply1.getAvatar());
                recursively(reply1);
            }
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
    }

    private void recursively(Comment comment) {
        tempReplys.add(comment);
        if (comment.getReplyComments().size()>0){
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply:replys) {
                reply.setRealAvatar(fastdfsDomaint + reply.getAvatar());
                tempReplys.add(reply);
                if (reply.getReplyComments().size()>0){
                    recursively(reply);
                }
            }
        }
    }

    // 存放迭代出现的饿所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    @Transactional
    @Override
    public Comment saveComent(Comment comment) {
        Long parentCommentId = comment.getParentComment().getId();
        if (parentCommentId != -1){
            // 如果不等于-1说明是点击回复评论，故而设置其级联的对象
            comment.setParentComment(commentRepository.getOne(parentCommentId));
        }else {
            // 如果是发起的评论这没有父评论
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentRepository.save(comment);
    }
}
